package rmhc.gui
{
	import flash.display.BitmapData;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	
	import rmhc.RMHCApp;
	import rmhc.api.dataflow.PotatoeInfo;
	import rmhc.design.StatusSelectDesign;

	public class StatusLane extends StatusSelectDesign
	{
		protected var grid:ScrollableGrid;
		protected var cells:Vector.<StatusCell>;
		protected var cellCount:int;
		
		protected var active:StatusCell;
		protected var dragging:Boolean;
		
		public function StatusLane()
		{
			super();
			grid = new ScrollableGrid(1, 80, 50, 6);
			grid.y = 13;
			addChildAt( grid , getChildIndex( btnNext ) );
		}
		
		public function init():void
		{
			//грузим xml-ку
			var url:String = RMHCApp.getServerUrl() + "potatoe.xml";
			var req:URLRequest = new URLRequest( url );
			var ul:URLLoader = new URLLoader();
			ul.addEventListener(Event.COMPLETE , onLoadXml );
			ul.load( req );
		}
		protected function onLoadXml( e:Event ):void
		{
			var ul:URLLoader = e.target as URLLoader;
			var data:XML = new XML( ul.data );
			
			cells = new Vector.<StatusCell>();
			cellCount = data.status.length();
			var cell:StatusCell;
			for each ( var node:XML in data.status )
			{
				cell = new StatusCell( parsePotatoe( node ) );
				cells.push( cell );
				cell.addEventListener( Event.COMPLETE , initNextCell );
				cell.addEventListener(MouseEvent.CLICK , onCellClick );
				//добавляем в сетку
				grid.addCell( cell );
			}
			if ( cells.length == 0 )
			{
				return;
			}
			
			//загружаем
			cells[0].init();
			//создаём скролл
			grid.setMaskArea( area );
			grid.setScrollSlider( barSlider );
			grid.initScroll();
			
			initUi();
		}
		protected function initNextCell( e:Event = null ):void
		{
			var i:int = 0;
			if ( e != null )
			{
				var cell:StatusCell = e.target as StatusCell;
				i = cells.indexOf( cell );
				//trace( "cell" , i , "inited, id:" , cell.getInfo().id );
			}
			if ( i + 1 >= cells.length )
			{
				//done
			}
			else
			{
				var next:StatusCell = cells[i + 1];
				next.init();
			}
		}
		protected function parsePotatoe( node:XML ):PotatoeInfo
		{
			var p:PotatoeInfo = new PotatoeInfo( node.id , node.thumb , node.pic );
			return p;
		}
		
		protected function onCellClick( e:MouseEvent ):void
		{
			var cell:StatusCell = e.currentTarget as StatusCell;
			if ( active == cell )
			{
				return;
			}
			
			if ( active != null )
			{
				active.setActive( false );
			}
			active = cell;
			active.setActive( true );
			dispatchEvent( new Event( Event.CHANGE ) );
		}
		
		protected function initUi():void
		{
			if ( grid.isEnabled() )
			{
				//ставим листенеры
				barSlider.addEventListener(MouseEvent.MOUSE_DOWN , onScrollDrag );
				stage.addEventListener(MouseEvent.MOUSE_UP , onScrollDrop );
				stage.addEventListener(MouseEvent.MOUSE_MOVE , onScrollMove );
				btnNext.addEventListener( MouseEvent.CLICK , onBtnNext );
				btnPrev.addEventListener( MouseEvent.CLICK , onBtnPrev );
				addEventListener( MouseEvent.MOUSE_WHEEL , onScrollWheel );
			}
			else
			{
				barSlider.visible = btnNext.visible = btnPrev.visible = false;
			}
		}
		protected function onBtnNext( e:MouseEvent ):void
		{
			//grid.scrollRight();
			grid.scrollOnce( 1 );
		}
		protected function onBtnPrev( e:MouseEvent ):void
		{
			//grid.scrollLeft();
			grid.scrollOnce( -1 );
		}
		protected function onScrollDrag( e:MouseEvent ):void
		{
			dragging = true;
			grid.dragSlider( true );
		}
		protected function onScrollDrop( e:MouseEvent ):void
		{
			grid.dragSlider( false );
			dragging = false;
		}
		protected function onScrollMove( e:MouseEvent ):void
		{
			if ( dragging )
			{
				grid.updateSliderPos( e );
			}
		}
		protected function onScrollWheel( e:MouseEvent ):void
		{
			//trace( "onScrollWheel" , e.delta );
			var d:Number = e.delta < 0 ? 1 : -1;
			grid.scrollOnce( d );
		}
		
		public function getActive():PotatoeInfo
		{
			if ( active != null )
			{
				return active.getInfo();
			}
			return null;
		}
		public function getThumb():BitmapData
		{
			if ( active != null )
			{
				return active.getThumb();
			}
			return null;
		}
		public function getThumbSrc():String
		{
			if ( active != null )
			{
				return active.getThumbSrc();
			}
			return null;
		}
	}
}